﻿
using System;
using System.Net;
namespace hutil {
    public class TimeUtil {
        public static int step=0;


        public static int getBit(int v, int bit)
        {
            return (v >> bit) & 1;
         }
      
    
        /// <summary>
        /// 时间戳转DateTime
        /// </summary>
        /// <returns></returns>
        public static DateTime TimeStamp2DT(string timeStamp) {
            return DateTime.Parse("1970-01-01 00:00:00").AddSeconds(long.Parse(timeStamp));
        }
        public static DateTime TimeStamp2DT(long timeStamp) {
            return DateTime.Parse("1970-01-01 00:00:00").AddSeconds(timeStamp);
        }
        /// <summary>
        /// 获取时间戳
        /// </summary>
        /// <returns></returns>
        public static long GetTimeStamp(System.DateTime time) {
            return ConvertDateTimeToInt(time);
        }
        /// <summary>  
        /// 将c# DateTime时间格式转换为Unix时间戳格式  
        /// </summary>  
        /// <param name="time">时间</param>  
        /// <returns>long</returns>  
        /// 
        public static DateTime baseTime = DateTime.Parse("1970-01-01 00:00:00");
        public static long ConvertDateTimeToInt(System.DateTime time) {
            //System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
            //long t = (time.Ticks - startTime.Ticks) / 10000000;   //除10000调整为13位      
            //return t;
            TimeSpan sp=time- baseTime;
            return (long)sp.TotalMilliseconds;
        }
        public static string seconds2DateSring(object timeStamp) {
            return seconds2Sring(timeStamp,"M-dd HH:mm:ss");
        }
        public static string seconds2TimeSring(object timeStamp) {
            return seconds2Sring(timeStamp,"HH:mm:ss");
        }
        private static string seconds2Sring(object timeStamp,String format) {
            if(timeStamp.GetType() == typeof(String)) {
                DateTime dt = TimeStamp2DT((String)timeStamp);
                return dt.ToString(format);
            } else if(timeStamp.GetType() == typeof(DateTime)) {
                return ((DateTime)timeStamp).ToString(format);
            } else {
                return timeStamp.ToString();
            }
        }
       
    }
}
